﻿using System;
using System.Drawing;
using System.Windows.Forms;

namespace FreeWorship
{
	public partial class SettingsForm : Form
	{
		Font verseFont=null;
		Font verseNumberFont=null;
		Font titleFont=null;
		Color titleColor;
		Color verseColor;
		Color verseNumberColor;
		Color backgroundColor;
		
		public SettingsForm()
		{
			InitializeComponent();
		}

		void ButtonCancelClick(object sender, EventArgs e)
		{
			this.Close();
		}
		
		void ButtonSetVerseFontClick(object sender, EventArgs e)
		{
			fontDialog.ShowColor=true;
			fontDialog.Font=verseFont;
			fontDialog.Color=verseColor;
			if(fontDialog.ShowDialog()!=DialogResult.Cancel)
			{
				verseFont = fontDialog.Font;
				verseColor=fontDialog.Color;
			}
		}
		
		
		void ButtonSetVerseNumberFontClick(object sender, EventArgs e)
		{
			fontDialog.ShowColor=true;
			fontDialog.Font=verseNumberFont;
			fontDialog.Color=verseNumberColor;
			if(fontDialog.ShowDialog()!=DialogResult.Cancel)
			{
				verseNumberFont = fontDialog.Font;
				verseNumberColor=fontDialog.Color;
			}
		}
		
		void ButtonOKClick(object sender, EventArgs e)
		{
			try{
				if (Convert.ToInt16(textBoxTopPercentage.Text) + Convert.ToInt16(textBoxBottomPercentage.Text) >= 100)
					throw new Exception();
			}catch(Exception)
			{
				MessageBox.Show("顶部留空比例或底部留空比例设置错误，请重新输入！");
				return;
			}
			if(verseColor.ToArgb()==backgroundColor.ToArgb())
			{
				MessageBox.Show("经文字体颜色设置与背景色相同，会导致经文不显示，请重新设置字体颜色！");
				return;
			}
			if(verseNumberColor.ToArgb()==backgroundColor.ToArgb())
			{
				MessageBox.Show("经文编号字体颜色设置与背景色相同，会导致经文编号不显示，请重新设置字体颜色");
				return;
			}
			if(titleColor.ToArgb()==backgroundColor.ToArgb())
			{
				MessageBox.Show("标题字体颜色设置与背景色相同，会导致标题不显示，请重新设置字体颜色");
				return;
			}

			settings.newLineEachVerse=checkBoxNewLineEachVerse.Checked;
			settings.printTitleAlways=checkBoxPrintTitleAtTopAlways.Checked;
			settings.printTitleAtTop=checkBoxPrintTitleAtTop.Checked;
			settings.printTitleAtBottom=checkBoxPrintTitleAtBottom.Checked;
			
			settings.topPercentage=Convert.ToInt16(textBoxTopPercentage.Text);
			settings.bottomPercentage=Convert.ToInt16(textBoxBottomPercentage.Text);
			
			settings.verseColor = verseColor.ToArgb();
			settings.verseNumberColor=verseNumberColor.ToArgb();
			settings.titleColor=titleColor.ToArgb();
			
			if(verseFont!=null)
			{
				settings.verseFontName=verseFont.Name;
				settings.verseFontSize=verseFont.Size;
				//MessageBox.Show(settings.verseFontName+" "+settings.verseFontSize);
			}
			
			if(verseNumberFont!=null)
			{
				settings.verseNumberFontSize = verseNumberFont.Size;
				settings.verseNumberFontName = verseNumberFont.Name;
			}
			
			if(titleFont!=null)
			{
				settings.titleFontName=titleFont.Name;
				settings.titleFontSize=titleFont.Size;
			}
			
			
			settings.backgroundColor = backgroundColor.ToArgb();
			
			this.Close();
			
		}
		
		private Settings settings = null;
		public void SetSettings(Settings settings)
		{
			this.settings=settings;
			
			titleColor = Color.FromArgb(settings.titleColor);
			verseColor = Color.FromArgb(settings.verseColor);
			verseNumberColor = Color.FromArgb(settings.verseNumberColor);
			backgroundColor = Color.FromArgb(settings.backgroundColor);
			
			titleFont = new Font(settings.titleFontName,settings.titleFontSize);
			verseFont = new Font(settings.verseFontName,settings.verseFontSize);
			verseNumberFont = new Font(settings.verseNumberFontName,settings.verseNumberFontSize);
			
			textBoxTopPercentage.Text = settings.topPercentage.ToString();
			textBoxBottomPercentage.Text = settings.bottomPercentage.ToString();
			
			checkBoxNewLineEachVerse.Checked = settings.newLineEachVerse;
			checkBoxPrintTitleAtBottom.Checked = settings.printTitleAtBottom;
			checkBoxPrintTitleAtTop.Checked = settings.printTitleAtTop;
			checkBoxPrintTitleAtTopAlways.Checked = settings.printTitleAlways;
		}
		
		void ButtonSetTitleFontClick(object sender, EventArgs e)
		{
			fontDialog.ShowColor=true;
			fontDialog.Font=titleFont;
			fontDialog.Color=titleColor;
			if(fontDialog.ShowDialog()!=DialogResult.Cancel)
			{
				titleFont = fontDialog.Font;
				
				titleColor=fontDialog.Color;
			}
		}
		
		void ButtonSetBackgroundColorClick(object sender, EventArgs e)
		{
			colorDialog.Color=backgroundColor;
			if(colorDialog.ShowDialog()!=DialogResult.Cancel)
			{
				backgroundColor = colorDialog.Color;
			}
		}
	}
}
